package io.bdmc.config;

import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import lombok.Getter;

@Getter
@Configuration
public class MqttConfig {
    @Value("${mqtt.host}")
    private String host;

    @Value("${mqtt.username}")
    private String username;

    @Value("${mqtt.password}")
    private String password;

    @Value("${mqtt.cleanSession}")
    private Boolean cleanSession;

    @Value("${mqtt.timeout}")
    private int timeout;

    @Value("${mqtt.clientId}")
    private String clientId;

    @Value("${mqtt.serverTopic}")
    private String serverTopic;

    @Value("${mqtt.clientTopic}")
    private String clientTopic;

    @Value("${mqtt.qos}")
    private int qos;

    @Bean
    public MqttConnectOptions mqttConnectOptions() {
        MqttConnectOptions options = new MqttConnectOptions();
        options.setServerURIs(new String[] { host });

        // 设置用户名和密码
        options.setUserName(username);
        options.setPassword(password.toCharArray());
        options.setCleanSession(cleanSession);
        options.setConnectionTimeout(timeout);
        options.setAutomaticReconnect(true);
        options.setConnectionTimeout(10); // in seconds
        options.setKeepAliveInterval(60); // in seconds

        // 其他连接配置...
        return options;
    }
}
